from django.db import models
from django.core.validators import URLValidator
from django.utils.html import format_html

class Movie(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    release_year = models.PositiveIntegerField()
    
    poster_url = models.URLField(
        max_length=500,
        blank=True,
        help_text="请输入完整图片URL（如https://example.com/poster.jpg）"
    )
    
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.title} ({self.release_year})"

    def poster_preview(self):
        """Admin中使用的预览方法"""
        if self.poster_url:
            return format_html('<img src="{}" width="100" />', self.poster_url)
        return "无海报"
    poster_preview.short_description = '海报预览'
